package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.common.constant.SharesConstan;
import com.common.utils.DateUtil;
import com.common.utils.HttpClient5Utils;
import com.common.utils.MeUtils;
import com.common.utils.StringUtil;
import com.entity.*;
import com.mapper.SharesReviewMapper;
import com.service.HintSharesService;
import com.service.SharesReviewService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.vo.SharesHyVsggVo;
import com.vo.SharesZdfCopyVo;
import com.vo.SharesZjlx;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wty
 * @since 2021-12-02
 */
@Service
public class SharesReviewServiceImpl extends ServiceImpl<SharesReviewMapper, SharesReview> implements SharesReviewService {

    @Autowired
    private HintSharesService hintSharesService;

    /**
     * 保存复盘数据
     */
    @Override
    public synchronized void saveData() {

        Wrapper<HintShares> wr=new EntityWrapper<>();
        wr.eq("time",DateUtil.getDate_yyyyMMdd());
        List<HintShares> voHintShares = hintSharesService.selectList(wr);
        Map<String,HintShares> hintSharesMap=new HashMap<>();
        for (HintShares voHintShare : voHintShares) {
            String code = voHintShare.getCode();
            hintSharesMap.put(code,voHintShare);
        }

        List<HintShares> upList=new ArrayList<>();

        String url="http://124.220.197.73:9200/shares/getHiCode";
        try {
            HttpGet get=new HttpGet(url);
            CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
            String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
            JSONObject datas2 = JSONObject.parseObject(result);
            String data = datas2.getString("data");
            List<HintShares> hintSharesList = JSONArray.parseArray(data, HintShares.class);
            List<SharesReview> reviewList=new ArrayList<>();
            for (HintShares hintShares : hintSharesList) {

                String newZdf = hintShares.getNewZdf();
                if(hintShares.getTsOrder()>20){
                    if(StringUtil.isEmpty(newZdf)||new BigDecimal(newZdf).compareTo(new BigDecimal(10))==-1){
                        continue;
                    }
                }

                SharesReview sharesReview = new SharesReview();
                sharesReview.setCode(hintShares.getCode());

                int order = hintShares.getTsOrder();
                sharesReview.setOrder(order);
                sharesReview.setName(hintShares.getName());
                sharesReview.setCode(hintShares.getCode());
                if(null!=hintShares.getTsprice()){
                    sharesReview.setTsprice(hintShares.getTsprice().toString());
                }
                sharesReview.setTime(hintShares.getTime());
                sharesReview.setZhint(hintShares.getZhint());
                sharesReview.setYhint(hintShares.getYhint());
                sharesReview.setType(hintShares.getType());
                sharesReview.setSgin(hintShares.getSgin());
                sharesReview.setKeepNum(hintShares.getKeepNum());
                sharesReview.setKeepTotal(hintShares.getKeepTotal());
                sharesReview.setNewZdf(hintShares.getNewZdf());

                //更新当天数据
                HintShares upVo = hintSharesMap.get(hintShares.getCode());
                if(null!=upVo){
                    upVo.setKeepNum(hintShares.getKeepNum());
                    upVo.setKeepTotal(hintShares.getKeepTotal());
                    upVo.setNewZdf(hintShares.getNewZdf());
                    upList.add(upVo);
                }

                if(null!=hintShares.getCPrice()){
                    sharesReview.setCprice(hintShares.getCPrice().toString());
                }
                sharesReview.setTsZdf(hintShares.getTsZdf());
                if(null!=hintShares.getNewPrice()){
                    sharesReview.setNewPrice(hintShares.getNewPrice().toString());
                }
                try {
                    sharesReview.setSrcf(MeUtils.imgBase64(hintShares.getSrcf()));
                    sharesReview.setSrc5(MeUtils.imgBase64(hintShares.getSrc5()));
                    sharesReview.setSrcr(MeUtils.imgBase64(hintShares.getSrcr()));
                    sharesReview.setSrcz(MeUtils.imgBase64(hintShares.getSrcz()));
                    sharesReview.setSrcy(MeUtils.imgBase64(hintShares.getSrcy()));
                }catch (Exception e){
                    System.out.println("图片转换err");
                }


                sharesReview.setZyPrice(hintShares.getZyPrice());
                sharesReview.setZback(hintShares.getZback());
                sharesReview.setYback(hintShares.getYback());
                sharesReview.setUnfastenDay(hintShares.getUnfastenDay());
                if(!CollectionUtils.isEmpty(hintShares.getUnfastenList())){
                    sharesReview.setUnfastenList(JSONObject.toJSONString(hintShares.getUnfastenList()));
                }
                sharesReview.setSshy(hintShares.getSshy());
                sharesReview.setZzfsign(hintShares.getZzfsign());
                sharesReview.setZzdfsub(hintShares.getZzdfsub());
                sharesReview.setMaxzzdf(hintShares.getMaxzzdf());
                sharesReview.setYzdfsub(hintShares.getYzdfsub());
                sharesReview.setMaxyzdf(hintShares.getMaxyzdf());
                if(!CollectionUtils.isEmpty(hintShares.getZvoList())){
                    sharesReview.setZvoList(JSONObject.toJSONString(hintShares.getZvoList()));
                }
                sharesReview.setYzfsign(hintShares.getYzfsign());
                if(!CollectionUtils.isEmpty(hintShares.getSharesHyzxes())){
                    sharesReview.setSharesHyzxes(JSONObject.toJSONString(hintShares.getSharesHyzxes()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getNotices())){
                    sharesReview.setNotices(JSONObject.toJSONString(hintShares.getNotices()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesDirections())){
                    sharesReview.setSharesDirections(JSONObject.toJSONString(hintShares.getSharesDirections()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getYvoList())){
                    sharesReview.setYvoList(JSONObject.toJSONString(hintShares.getYvoList()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesBgczs())){
                    sharesReview.setSharesBgczs(JSONObject.toJSONString(hintShares.getSharesBgczs()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesYjygs())){
                    sharesReview.setSharesYjygs(JSONObject.toJSONString(hintShares.getSharesYjygs()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesYjbgs())){
                    sharesReview.setSharesYjbgs(JSONObject.toJSONString(hintShares.getSharesYjbgs()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesGdnums())){
                    sharesReview.setSharesGdnums(JSONObject.toJSONString(hintShares.getSharesGdnums()));
                }
                if(!CollectionUtils.isEmpty(hintShares.getSharesGdChanges())){
                    sharesReview.setSharesGdChanges(JSONObject.toJSONString(hintShares.getSharesGdChanges()));
                }
                if(null!=hintShares.getBkzj()){
                    sharesReview.setBkzj(JSONObject.toJSONString(hintShares.getBkzj()));
                }
                if(null!=hintShares.getGgzj()){
                    sharesReview.setGgzj(JSONObject.toJSONString(hintShares.getGgzj()));
                }
                if(null!=hintShares.getGgsyl()){
                    sharesReview.setGgsyl(JSONObject.toJSONString(hintShares.getGgsyl()));
                }
                if(null!=hintShares.getHysyl()){
                    sharesReview.setHysyl(JSONObject.toJSONString(hintShares.getHysyl()));
                }
                sharesReview.setSshy(hintShares.getSshy());
                sharesReview.setSshyPercent(hintShares.getSshyPercent());
                sharesReview.setSsgn(hintShares.getSsgn());
                sharesReview.setTjDate(hintShares.getTjDate());

                reviewList.add(sharesReview);
            }
            if(!CollectionUtils.isEmpty(reviewList)){
                List<SharesReview> collect = reviewList.stream().sorted(Comparator.comparing(SharesReview::getOrder)).collect(Collectors.toList());
                this.insertBatch(collect);
            }

            if(!CollectionUtils.isEmpty(upList)){
                hintSharesService.updateBatchById(upList);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //去重
        Wrapper<SharesReview> wrdis=new EntityWrapper<>();
        wrdis.eq("time",DateUtil.getDate_yyyyMMdd());
        List<SharesReview> reviewList = this.selectList(wrdis);
        Map<String,SharesReview> disMap=new HashMap<>();

        List<SharesReview> delList = new ArrayList<>();
        for (SharesReview sharesReview : reviewList) {
            String code = sharesReview.getCode();
            SharesReview vo = disMap.get(code);
            if(vo==null){
                disMap.put(code,sharesReview);
            }else {
                delList.add(sharesReview);
            }
        }

        if(!CollectionUtils.isEmpty(delList)){
            for (SharesReview sharesReview : delList) {
                this.deleteById(sharesReview.getId());
            }
        }

    }


    @Override
    public List<HintShares> changeHi(List<SharesReview> reviewList) {

        List<HintShares> hintSharesList=new ArrayList<>();
        for (SharesReview sharesReview : reviewList) {
            HintShares hintShares = new HintShares();
            hintShares.setId(sharesReview.getId());
            hintShares.setCode(sharesReview.getCode());
            hintShares.setName(sharesReview.getName());
            String bkzj = sharesReview.getBkzj();
            if(!StringUtil.isEmpty(bkzj)){
                SharesZjlx sharesZjlx = JSON.parseObject(bkzj, SharesZjlx.class);
                hintShares.setBkzj(sharesZjlx);
            }
            String cprice = sharesReview.getCprice();
            if(!StringUtil.isEmpty(cprice)){
                hintShares.setCPrice(new BigDecimal(cprice));
            }

            String ggsyl = sharesReview.getGgsyl();
            if(!StringUtil.isEmpty(ggsyl)){
                SharesHyVsggVo sharesHyVsggVo = JSON.parseObject(ggsyl, SharesHyVsggVo.class);
                hintShares.setGgsyl(sharesHyVsggVo);
            }
            String hysyl = sharesReview.getHysyl();
            if(!StringUtil.isEmpty(hysyl)){
                SharesHyVsggVo sharesHyVsggVo = JSON.parseObject(hysyl, SharesHyVsggVo.class);
                hintShares.setHysyl(sharesHyVsggVo);
            }
            String ggzj = sharesReview.getGgzj();
            if(!StringUtil.isEmpty(ggzj)){
                SharesZjlx sharesZjlx = JSON.parseObject(ggzj, SharesZjlx.class);
                hintShares.setGgzj(sharesZjlx);
            }
            hintShares.setKeepNum(sharesReview.getKeepNum());
            hintShares.setKeepTotal(sharesReview.getKeepTotal());
            hintShares.setMaxyzdf(sharesReview.getMaxyzdf());
            hintShares.setMaxzzdf(sharesReview.getMaxzzdf());
            String newPrice = sharesReview.getNewPrice();
            if(!StringUtil.isEmpty(newPrice)){
                hintShares.setNewPrice(new BigDecimal(newPrice));
            }
            hintShares.setNewZdf(sharesReview.getNewZdf());
            String notices = sharesReview.getNotices();
            if(!StringUtil.isEmpty(notices)){
                List<SharesNotice> sharesNotices = JSONObject.parseArray(notices, SharesNotice.class);
                hintShares.setNotices(sharesNotices);
            }
            hintShares.setTsOrder(sharesReview.getOrder());
            hintShares.setSgin(sharesReview.getSgin());
            String sharesBgczs = sharesReview.getSharesBgczs();
            if(!StringUtil.isEmpty(sharesBgczs)){
                List<SharesBgcz> sharesBgczs1 = JSONObject.parseArray(sharesBgczs, SharesBgcz.class);
                hintShares.setSharesBgczs(sharesBgczs1);
            }
            String sharesDirections = sharesReview.getSharesDirections();
            if(!StringUtil.isEmpty(sharesDirections)){
                List<SharesDirection> sharesDirections1 = JSONObject.parseArray(sharesDirections, SharesDirection.class);
                hintShares.setSharesDirections(sharesDirections1);
            }
            String sharesGdChanges = sharesReview.getSharesGdChanges();
            if(!StringUtil.isEmpty(sharesGdChanges)){
                List<SharesGdChange> sharesGdChanges1 = JSONObject.parseArray(sharesGdChanges, SharesGdChange.class);
                hintShares.setSharesGdChanges(sharesGdChanges1);
            }
            String sharesGdnums = sharesReview.getSharesGdnums();
            if(!StringUtil.isEmpty(sharesGdnums)){
                List<SharesGdnum> gdnums = JSONObject.parseArray(sharesGdnums, SharesGdnum.class);
                hintShares.setSharesGdnums(gdnums);
            }
            String sharesHyzxes = sharesReview.getSharesHyzxes();
            if(!StringUtil.isEmpty(sharesHyzxes)){
                List<SharesHyzx> sharesHyzxes1 = JSONObject.parseArray(sharesHyzxes, SharesHyzx.class);
                hintShares.setSharesHyzxes(sharesHyzxes1);
            }

            String sharesYjbgs = sharesReview.getSharesYjbgs();
            if(!StringUtil.isEmpty(sharesYjbgs)){
                List<SharesYjbg> sharesYjbgs1 = JSONObject.parseArray(sharesYjbgs, SharesYjbg.class);
                hintShares.setSharesYjbgs(sharesYjbgs1);
            }
            String sharesYjygs = sharesReview.getSharesYjygs();
            if(!StringUtil.isEmpty(sharesYjygs)){
                List<SharesYjyg> sharesYjygs1 = JSONObject.parseArray(sharesYjygs, SharesYjyg.class);
                hintShares.setSharesYjygs(sharesYjygs1);
            }
            hintShares.setSrc5(sharesReview.getSrc5());
            hintShares.setSrcf(sharesReview.getSrcf());
            hintShares.setSrcr(sharesReview.getSrcr());
            hintShares.setSrcy(sharesReview.getSrcy());
            hintShares.setSrcz(sharesReview.getSrcz());
            hintShares.setSshy(sharesReview.getSshy());
            hintShares.setSsgn(sharesReview.getSsgn());
            hintShares.setSshyPercent(sharesReview.getSshyPercent());
            hintShares.setTjDate(sharesReview.getTjDate());
            hintShares.setTsZdf(sharesReview.getTsZdf());
            String tsprice = sharesReview.getTsprice();
            if(!StringUtil.isEmpty(tsprice)){
                hintShares.setTsprice(new BigDecimal(tsprice));
            }
            hintShares.setType(sharesReview.getType());
            hintShares.setUnfastenDay(sharesReview.getUnfastenDay());
            String unfastenList = sharesReview.getUnfastenList();
            if(!StringUtil.isEmpty(unfastenList)){
                List<String> strings = JSONObject.parseArray(unfastenList, String.class);
                hintShares.setUnfastenList(strings);
            }
            hintShares.setYback(sharesReview.getYback());
            hintShares.setYhint(sharesReview.getYhint());
            String yvoList = sharesReview.getYvoList();
            if(!StringUtil.isEmpty(yvoList)){
                List<SharesZdfCopyVo> sharesZdfCopyVos = JSONObject.parseArray(yvoList, SharesZdfCopyVo.class);
                hintShares.setYvoList(sharesZdfCopyVos);
            }
            hintShares.setYzdfsub(sharesReview.getYzdfsub());
            hintShares.setYzfsign(sharesReview.getYzfsign());
            hintShares.setZback(sharesReview.getZback());
            hintShares.setZhint(sharesReview.getZhint());
            String zvoList = sharesReview.getZvoList();
            if(!StringUtil.isEmpty(zvoList)){
                List<SharesZdfCopyVo> sharesZdfCopyVos = JSONObject.parseArray(zvoList, SharesZdfCopyVo.class);
                hintShares.setZvoList(sharesZdfCopyVos);
            }
            hintShares.setZyPrice(sharesReview.getZyPrice());
            hintShares.setZzdfsub(sharesReview.getZzdfsub());
            hintShares.setZzfsign(sharesReview.getZzfsign());

            hintSharesList.add(hintShares);
        }

        List<HintShares> collect = hintSharesList.stream().sorted(Comparator.comparing(HintShares::getTsOrder)).collect(Collectors.toList());
        return collect;
    }
}
